import beans.QueryParamInfo;
import org.apache.log4j.PropertyConfigurator;
import quartz.QuartzManager;
import quartz.SearchJob;
import quartz.TokenJob;
import service.APIService;
import util.ParamUtil;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * Created by yanglq on 2018/4/8.
 */
public class Main {


    public static void main(String[] args) {
        Date currenTime = new Date();
        //加载日志系统
        Properties props = new Properties();
        try {
            props.load(Main.class.getClassLoader().getResourceAsStream("log4j.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        PropertyConfigurator.configure(props);

        try {
            //加载查询参数
            ParamUtil paramUtil = new ParamUtil();
            QueryParamInfo info = paramUtil.initConfig();

            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(currenTime);
//            info.setQueryTimeBegin(dateString);
            info.setQueryTimeBegin("2018-03-28 00:00:00");
//            info.setPageSize(100);
            //初始化系统参数
            APIService apiService = new APIService();
            apiService.initConfig();
            try {
                //添加获取token任务
                doGetTOkenJob();
                //添加查询任务
                doSearchJob(info);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void doSearchJob(QueryParamInfo info) throws InterruptedException {
        Class<SearchJob> jobs = SearchJob.class;
        String job_name = "getCarAccess";
        if (info != null) {
            System.out.println("【查询任务启动】开始(每隔" + info.getQueryTimeInterval() + "分钟查询一次)");
            if (info.getQueryTimeInterval() != null) {
                String time = "0 0/" + info.getQueryTimeInterval() + " * * * ?";
//                String time = "15 * * * * ?";
                QuartzManager.addJob(job_name, jobs, time, info);
            } else {
                String time = "0 0/5 * * * ?";
                QuartzManager.addJob(job_name, jobs, time, info);
            }
        }
    }

    public static void doGetTOkenJob() throws InterruptedException {
        Class<TokenJob> jobs = TokenJob.class;
        String job_name = "getToken";
        System.out.println("【获取token任务启动】开始(每隔2小时查询一次)");
        QuartzManager.addJob(job_name, jobs);
    }

}
